package run.siyuan.common.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import run.siyuan.common.constant.ResultCodeEnum;

/**
 * @program: education-platform
 * @description:
 * @author: SY
 * @create: 2020-12-24 10:14
 **/
@Data
@ApiModel(value = "全局统一返回结果")
public class Result<T> {

    @ApiModelProperty(value = "返回码")
    private Integer code = 20000;

    @ApiModelProperty(value = "返回消息")
    private String message = "success";

    @ApiModelProperty(value = "返回数据")
    private T data;

    public Result(){}

    public static Result success(){
        Result r = new Result();
        r.setCode(20000);
        r.setMessage("success");
        return r;
    }

    public static Result error(Integer code,String message){
        Result r = new Result();
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    public static Result setResult(ResultCodeEnum resultCodeEnum){
        Result r = new Result();
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    public Result data(T map){
        this.setData(map);
        return this;
    }
}
